package com.youmu.shop.cart.controller;


import java.util.List;

import javax.annotation.Resource;
import javax.security.auth.login.LoginException;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.exception.ParamException;
import com.result.Result;
import com.result.ResultUtils;
import com.youmu.shop.cart.domain.Cart;
import com.youmu.shop.cart.service.CartService;
import com.youmu.shop.cart.vo.CartVO;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;


@RestController
@RequestMapping(value = "/cart/remote/")
public class CartController {

    
	@Resource
    private CartService cartService;
    
	@ApiOperation(value = "获取购物车")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "long", paramType = "query"),
	})
   	@RequestMapping(value = "findCart",method=RequestMethod.GET)
   	public Result<CartVO> findCart(Long userId) {
		if(userId == null){
			throw new ParamException("userId不能为空");
		}
       	List<CartVO>  cartVOs = cartService.findCart(userId);
       	return ResultUtils.createDefResult(cartVOs);
   	}
	
	@ApiOperation(value = "添加购物车")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "productId", value = "商品id", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "specificationId", value = "商品规格", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "number", value = "数量", required = true, dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "remark", value = "备注", required = true, dataType = "String", paramType = "query"),
	})
   	@RequestMapping(value = "saveCart",method=RequestMethod.POST)
   	public Result<Long> saveCart(Cart cart) {
		if(cart == null || cart.getUserId() == null){
			throw new ParamException("userId不能为空");
		}
       	long cartId = cartService.saveCart(cart);
       	return ResultUtils.createDefResult(cartId);
   	}
	
	@ApiOperation(value = "修改购物车")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "number", value = "数量", required = true, dataType = "int", paramType = "query"),
	})
   	@RequestMapping(value = "updateCart",method=RequestMethod.POST)
   	public Result updateCart(Cart cart) {
		if(cart == null || cart.getId()== null){
			throw new ParamException("id不能为空");
		}
       	cartService.updateCart(cart);
       	return ResultUtils.createNullResult();
   	}
	
	@ApiOperation(value = "删除购物车")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "long", paramType = "query"),
	})
   	@RequestMapping(value = "deleteCart",method=RequestMethod.POST)
   	public Result deleteCart(Long id) {
		if(id== null){
			throw new ParamException("id不能为空");
		}
       	cartService.deleteCart(id);
       	return ResultUtils.createNullResult();
   	}
	
	
}